import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import { getItem } from '@/utils/storage'

const routes = [
	{
		path: '/',
		component: () => import('@/views/layout'),
		children: [
      // 首页
			{
				path: '', // 默认子路由
				name: 'home',
				component: () => import('@/views/home'),
			},
      // 分类
			{
				path: '/cate',
				name: 'cate',
				component: () => import('@/views/cate'),
			},
      // 专题
			// {
			// 	path: '/subject',
			// 	name: 'subject',
			// 	component: () => import('@/views/subject'),
			// 	children: [],
			// },
      // 购物车
			{
				path: '/cart',
				name: 'cart',
				component: () => import('@/views/cart'),
			},
      // 我的
			{
				path: '/my',
				name: 'my',
				component: () => import('@/views/user/index'),
			},
		],
	},
  // 登录
	{
		path: '/login',
		name: 'login',
		component: () => import('@/views/user/login'),
	},
  // 注册
	{
		path: '/register',
		name: 'register',
		component: () => import('@/views/user/register'),
	},
  // 商品类别
	{
		path: '/goods',
		name: 'goods',
		component: () => import('@/views/goods/list'),
	},
  // 商品详情
	{
		path: '/goods/detail',
		name: 'goodsDetail',
		component: () => import('@/views/goods/detail'),
	},
  // 热门商品
	{
		path: '/goods/hot',
		name: 'goodsHot',
		component: () => import('@/views/goods/hot'),
	},
  // 新品
	{
		path: '/goods/new',
		name: 'goodsNew',
		component: () => import('@/views/goods/new'),
	},
  // 商品搜索
	{
		path: '/search',
		name: 'search',
		component: () => import('@/views/goods/search'),
	},
  // 地址列表
	{
		path: '/address',
		name: 'address',
		component: () => import('@/views/user/address'),
		meta: { needLogin: true },
	},
  // 地址详情
	{
		path: '/address/info',
		name: 'addressInfo',
		component: () => import('@/views/user/address/info'),
		meta: { needLogin: true },
	},
  // 我的足迹
	{
		path: '/footprint',
		name: 'footprint',
		component: () => import('@/views/user/footprint'),
		meta: { needLogin: true },
	},
  // 我的收藏
	{
		path: '/collect',
		name: 'collect',
		component: () => import('@/views/user/collect'),
		meta: { needLogin: true },
	},
  // 品牌列表
	{
		path: '/brand',
		name: 'brand',
		component: () => import('@/views/brand/list'),
	},
  // 品牌详情
	{
		path: '/brand/detail',
		name: 'brandDetail',
		component: () => import('@/views/brand/detail'),
	},
  // 用户关注品牌
  {
		path: '/brand/user',
		name: 'brandUser',
		component: () => import('@/views/user/brand'),
	},

  // 确认订单
	{
		path: '/checkout',
		name: 'checkout',
		component: () => import('@/views/buy/checkout'),
		meta: { needLogin: true },
	},
  // 提交订单
	{
		path: '/submit',
		name: 'submit',
		component: () => import('@/views/buy/submit'),
		meta: { needLogin: true },
	},
  // 支付
	{
		path: '/pay',
		name: 'pay',
		component: () => import('@/views/buy/pay'),
		meta: { needLogin: true },
	},
  // 支付失败
	{
		path: '/failpay',
		name: 'failpay',
		component: () => import('@/views/buy/failpay'),
		meta: { needLogin: true },
	},
  // 订单列表
	{
		path: '/order',
		name: 'order',
		component: () => import('@/views/user/order'),
		meta: { needLogin: true },
	},
  // 售后服务
  {
		path: '/service',
		name: 'service',
		component: () => import('@/views/user/service'),
		meta: { needLogin: true },
	},
]

const router = new VueRouter({
	routes,
})

router.beforeEach((to, from, next) => {
	window.scroll(0, 0)
	let isLogin = getItem('SHOP_TOKEN')
	if (to.meta.needLogin) {
		if (isLogin) {
			next()
		} else {
			next('/login?redirect=' + to.fullPath)
		}
	} else {
		next()
	}
})

export default router
